package com.xiang.community.init;

import com.xiang.community.dao.elasticsearch.DiscussPostRepository;
import com.xiang.community.entity.DiscussPost;
import com.xiang.community.service.DiscussPostService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author qixiangLi
 * @Date 2021/12/26
 */
@Slf4j
@Component
public class ElasticSearchInit implements CommandLineRunner {
    @Resource
    private DiscussPostService discussPostService;
    @Resource
    private DiscussPostRepository discussPostRepository;

    @Override
    public void run(String... args) throws Exception {
        log.info("获取帖子存入ES...");
        List<DiscussPost> list = discussPostService.findAll();
        for (DiscussPost discussPost : list) {
            discussPostRepository.save(discussPost);
        }
        log.info("帖子存入ES完成");
    }
}
